package io.kiki.sba.registry.server.shared.resource;

import io.kiki.sba.registry.common.model.GenericResponse;
import io.kiki.sba.registry.common.model.Response;
import io.kiki.sba.registry.server.shared.meta.MetaServerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;


@Path("opsapi/v1/")
public class RegistryOpsResource {

    private static final Logger logger = LoggerFactory.getLogger(RegistryOpsResource.class);

    @Autowired
    MetaServerService metaServerService;

    @PUT
    @Path("/server/group/quit")
    @Produces(MediaType.APPLICATION_JSON)
    public Response kickOffMyself() {
        logger.info("[kickOffMyself] begin");
        Response response = null;
        try {
            metaServerService.addSelfToMetaBlacklist();
            response = GenericResponse.buildSuccessResponse();
        } catch (Throwable th) {
            logger.error("[kickOffMyself]", th);
            response = GenericResponse.buildFailedResponse(th.getMessage());
        }
        logger.info("[kickOffMyself] end, response: {}", response);
        return response;
    }

    @PUT
    @Path("/server/group/join")
    @Produces(MediaType.APPLICATION_JSON)
    public Response putMyselfBack() {
        logger.info("[putMyselfBack] begin");
        Response response = null;
        try {
            metaServerService.removeSelfFromMetaBlacklist();
            response = GenericResponse.buildSuccessResponse();
        } catch (Throwable th) {
            logger.error("[putMyselfBack]", th);
            response = GenericResponse.buildFailedResponse(th.getMessage());
        }
        logger.info("[putMyselfBack] end, response: {}", response);
        return response;
    }
}
